package com.googlecode.perfectable.persistable.session;

import javax.annotation.Nullable;

public class Sequence implements IdentifierGenerator {
	private static class SequenceIdentifier<T> implements Identifier<T> {
		private static final long serialVersionUID = 8278016325939121793L;

		private final int value;

		SequenceIdentifier(int value) {
			this.value = value;
		}

		@Override
		public String code() {
			return Integer.toString(this.value);
		}

		@Override
		public boolean equals(@Nullable Object obj) {
			if(this == obj) {
				return true;
			}
			if(!(obj instanceof SequenceIdentifier)) {
				return false;
			}
			SequenceIdentifier<?> other = (SequenceIdentifier<?>) obj;
			return this.value == other.value;
		}

		@Override
		public int hashCode() {
			return this.value;
		}

	}

	private int next = 1;

	@Override
	public <T> Identifier<T> generate() {
		Identifier<T> result = new SequenceIdentifier<>(this.next);
		this.next = this.next + 1;
		return result;
	}
}
